<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use App\Models\BookComment;

use App\Models\Category;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;

class BookCommentsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('系统评论列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('修改评论');

            $content->body($this->form()->edit($id));
        });
    }

    public function update($book_id,$id)
    {
        return $this->form()->update($id);
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create($book_id)
    {
        return Admin::content(function (Content $content) use ($book_id){

            $content->header('添加评论');
            //$content->description('description');

            $content->body($this->form($book_id));
        });
    }

    public function store($book_id)
    {
        return $this->form($book_id)->store();
    }

    public function destroy($book_id,$id)
    {
        //$this->form()->destroy()
        BookComment::query()->where('book_id',$book_id)->where('id',$id)->update(['deleted'=>1]);
        return ['status'=>true,'message'=>'删除成功'];
    }

    public function show($id, Content $content)
    {
        return $content
            ->header('评论详情')
            //->description('description')
            ->body($this->detail($id));
    }

    public function detail($id)
    {
        $show = new Show(Book::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                //$tools->disableList();
                $tools->disableDelete();
            });

        $show->image('图片封面')->image(config('app.qiniu_image_domain').'/');
        $show->title('商品名称');
        $show->field('user_book_comments','系统评论总数')->as(function () use ($id){
            return BookComment::query()->where('book_id',$id)->where('type',2)->where('deleted',0)->count();
        });
        $show->douban_score('豆瓣分');

        $show->book_publish_info('出版信息', function ($book_publish_info) {
            $book_publish_info->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableList();
                    $tools->disableDelete();
                });
            $book_publish_info->isbn('ISBN');
        });

        $show->divider();

        $show->comments('系统评论列表', function ($comments) {

            $comments->model()->where('type', '=', 2)->where('deleted',0);

            $comments->user()->wechat_nickname('用户昵称');
            $comments->content('评价内容')->style('width:650px');
            $comments->rate('评分');
            $comments->good_num('点赞数');

            //$comments->disableCreateButton();
            $comments->disableExport();
            $comments->disableRowSelector();
            //$comments->disableActions();
            $comments->disableFilter();

            $comments->actions(function ($actions) {
                //$actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();
            });
        });

        return $show;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Book::class, function (Grid $grid) {
            $grid->model()->where('deleted',0)->where('on_sale',1);

            $grid->image('图书封面')->image(config('app.qiniu_image_domain'), 100, 100);
            $grid->column('title', '图书名称')->style('width:200px');
            $grid->column('douban_score', '豆瓣分');
            $grid->column('book_publish_info.isbn','ISBN');
            $grid->column('book_comments_num','系统评论数')->display(function () {
                return BookComment::query()->where('book_id',$this->id)->where('type',2)->where('deleted',0)->count();
            });
            $grid->category_parent_id('一级分类')->display(function($value) {
                $category = Category::query()->where('id',$value)->first();
                if(!empty($category))
                {
                    return $category->title;
                }
                return '';
            });
            $grid->category_id('二级分类')->display(function($value) {
                $category = Category::query()->where('id',$value)->first();
                if(!empty($category))
                {
                    return $category->title;
                }
                return '';
            });
            /*$grid->model()->where('deleted',0)->where('type',2);
            $grid->user()->wechat_nickname('用户昵称');
            $grid->book()->title('图书名称');
            $grid->rate('评分')->display(function ($rate) {
                $html = "<i class='fa fa-star' style='color:#ff8913'></i>";

                return join('&nbsp;', array_fill(0, min(5, $rate), $html));
            });
            $grid->good_num('点赞数');
            $grid->status('审核状态')->display(function ($status){
                $str = '';
                switch ($status)
                {
                    case 1:
                        $str = '待审核';
                        break;
                    case 2:
                        $str = '已审核';
                        break;
                    case 3:
                        $str = '审核未过';
                        break;
                }
                return $str;
            });
            $grid->created_at('创建时间');*/
            //$grid->updated_at();
            /*$grid->tools(function ($tools) {
                $tools->append(new BookCommentStatus());
            });*/

            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->like('title', '商品名称');
                $filter->equal('category_parent_id', '一级分类')
                    ->select(Category::getSelectOptions())
                    ->load('category_id', '/api/admin/second/categories');

                $filter->equal('category_id', '二级分类')->select();

            });

            $grid->disableCreateButton();
            $grid->actions(function ($actions) {
                //$actions->disableView();
                $actions->disableDelete();
                $actions->disableEdit();
            });

            $grid->disableRowSelector();
            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($book_id)
    {
        return Admin::form(BookComment::class, function (Form $form) use ($book_id){
            $form->display('id', 'ID');

            //$form->select('user_id','所选用户')->options(User::getSelectOptions())->rules('required');
            /*$form->select('user_id','所选用户')->options(function ($id) {
                $user = User::find($id);

                if ($user) {
                    return [$user->id => $user->wechat_nickname];
                }
            })->ajax('/api/admin/users');*/

            $form->select('user_id', '所选用户')->options('/api/admin/users');

            /*$form->select('book_id','所选图书')->options(function ($book_id) {
                $book = Book::find($book_id);

                if ($book) {
                    return [$book->id => $book->title];
                }
            })->ajax('/api/admin/search/books');*/
            $book = Book::query()->where('id',$book_id)->first();
            $form->display('book_title','所选图书')->value($book->title);

            //$form->text('book_id','所选图书')->value($book->title)->attribute(['disabled' => 'true']);

            //$form->select('book_id','所选图书')->options('/api/admin/search/book/'.$book_id);

            $form->number('rate', '评分')->max(5)->min(1)->default(5);
            $form->number('good_num','点赞数')->default(0);
            $form->textarea('content','评价内容')->rows(5);

            //$form->radio('status','审核状态')->options(['1' => '待审核', '2'=> '已审核', '3'=>'审核未过'])->default('2');

            $form->hidden('type')->value(2);
            $form->hidden('book_id')->value($book->id);
            $form->hidden('status')->value(2);

            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });
            //$form->display('created_at', 'Created At');
            //$form->display('updated_at', 'Updated At');
        });
    }
}
